<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        body {
            text-align: center;
        }

        .mycanvas {
            background-color: blueviolet;
        }
    </style>
    <script>
        window.onload = function () {
            let myCanvas = document.querySelector('.mycanvas');
            let context = myCanvas.getContext('2d');

            let arr = [];

            function getrandom() {
                if (arr.length > 50) {
                    clearInterval(timer);
                }

                let r = Math.floor(Math.random() * 255);
                let g = Math.floor(Math.random() * 255);
                let b = Math.floor(Math.random() * 255);
                let a = Math.random();

                let bg = "rgba(" + 255 + "," + 255 + "," + 255 + " ," + a + ")";
                let xr = Math.floor(Math.random() * 20);
                let sx = -2 + Math.floor(Math.random() * 4)
                let sy = -2 + Math.floor(Math.random() * 4);
                let cx = 200 + Math.floor(Math.random() * 200);
                let cy = 200 + Math.floor(Math.random() * 200);

                let obj = {
                    bg: bg,
                    xr: xr,
                    sx: sx,
                    sy: sy,
                    cx: cx,
                    cy: cy
                }
                if (sx != 0 || sy != 0) {
                    arr.push(obj);
                }
            }

            let timer = setInterval(getrandom, 30)

            function shop() {
                context.clearRect(0, 0, 600, 600);
                for (var i = 0; i < arr.length; i++) {
                    if (arr[i].cx + arr[i].xr > 600 || arr[i].cx - arr[i].xr <= 0) {
                        // arr[i].sx *=-1;
                        arr[i].sx = arr[i].sx * -1
                    }
                    if (arr[i].cy + arr[i].xr > 600 || arr[i].cy - arr[i].xr <= 0) {
                        // arr[i].sx *=-1;
                        arr[i].sy = arr[i].sy * -1
                    }
                    arr[i].cx += arr[i].sx;
                    arr[i].cy += arr[i].sy;
                    context.beginPath();
                    context.arc(arr[i].cx, arr[i].cy, arr[i].xr, 0, Math.PI * 2, false);
                    context.fillStyle = arr[i].bg;
                    context.fill();
                    context.closePath();
                }
            }
            setInterval(shop, 30)
        }
    </script>
</head>

<body>
    <canvas class="mycanvas" width="600" height="600"></canvas>
</body>

</html>